using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NBear.Common;
using NBear.IoC.Service;

namespace NBear.WinTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ITestService obj = NBear.IoC.Service.ServiceFactory.Create().GetService<ITestService>();
            obj.Func1("p1");
            obj.Func2("p1", 2);

            //ISimpleService obj = new NBear.IoC.Service.ServiceFactory(new NBear.IoC.Service.SimpleServiceContainer(new NBear.IoC.Service.MemoryServiceMQ(), null)).GetService<ISimpleService>();
            //obj.Func();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Category obj = EntityFactory<Category>.CreateObject();
            //obj.CategoryName = "name";
            //obj.Description = "desc";
            //obj.Picture = new byte[] { 1, 2, 3 };

            //obj =(Category)TestSerialize(obj, obj.GetType());

            //object[] objs = new object[] { obj, obj, obj };
            //objs = (object[])TestSerializeArray(objs, typeof(Category[]));

            //object testobj = TestSerialize(new TestCombined(), typeof(TestCombined));

            ParameterCollection pc = new ParameterCollection();
            pc["p1"] = "p1";
            pc["p2"] = new EntitySerializer(EntitySerializeType.Json).Serialize(EntityFactory<Category>.CreateObject());

            try
            {
                pc = (ParameterCollection)TestSerialize(pc, pc.GetType());
            }
            catch //(Exception ex)
            {
            }
        }

        private object TestSerialize(object obj, Type type)
        {
            string str = SerializeHelper.Serialize(obj);
            MessageBox.Show(str);
            return SerializeHelper.Deserialize(type, str);
        }

        private object TestSerializeArray(object[] obj, Type type)
        {
            return SerializeHelper.Deserialize(Util.GetOriginalTypeOfArrayType(type).MakeArrayType(), SerializeHelper.SerializeArray(obj));
        }
    }

    public interface ITest : IEntity
    {
    }

    public interface ITestService : IServiceInterface
    {
        string Func1(string p1);

        bool Func2(string p1, int p2);
    }

    public interface ISimpleService
    {
        void Func();
    }

    public class TestService : NBear.IoC.Service.BaseServiceInterfaceImpl, ITestService
    {
        public TestService(NBear.IoC.Service.IServiceContainer container)
            : base(container, typeof(ITestService))
        {
        }

        #region ITestService Members

        public string Func1(string p1)
        {
            return (string)CallService("Func1", typeof(string), p1);
        }

        public bool Func2(string p1, int p2)
        {
            return (bool)CallService("Func2", typeof(bool), p1, p2);
        }

        #endregion
    }

    [Table("Categories")]
    public interface Category : IEntity
    {
        [PrimaryKey]
        int CategoryID { get; }
        string CategoryName { get; set; }
        string Description { get; set; }
        System.Byte[] Picture { get; set; }
    }
}